var ctx=window.daijia.ctx;

var offset=0;
var limit=20;
var pagingLimit=20;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数

var pageLoading=true;//是否能滚动加载
var hasShowPages=false;//是否已显示分页
var firstLoadPages=true;//分页初始化时 不自动加载

var search_khName=null;
var search_hfName=null;
var search_startTime=null;
var search_endTime=null;


$(function(){
	$("#startTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	$("#endTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });

	initdata(false);
	
	//高级搜索
	$(".query-jiant").click(function(){
		var src=$("#jiant").attr("src");
		if(src.indexOf("jiant2")>=0){
			$("#jiant").attr("src",ctx+"/assets/img/jiant.png");
			$(".choose").slideDown(300);
		}else{
			$("#jiant").attr("src",ctx+"/assets/img/jiant2.png");
			$(".choose").slideUp(300);
		}
	});
$("#searchButton").click(function(){
	      	
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$/;
		if($("#startTime").val()!=""){
			if(!DATE_FORMAT.test($("#startTime").val())){
			   $.scojs_message("您输入的启始日期格式有误，正确格式如：2012-01-01", $.scojs_message.TYPE_ERROR);
			   return false;
			}
		}
		
		if($("#endTime").val()!=""){
			if(!DATE_FORMAT.test($("#endTime").val())){
				$.scojs_message("您输入的结束日期格式有误，正确格式如：2012-01-01", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if($("#startTime").val()!="" && $("#endTime").val()!=""){
			if($("#startTime").val()>$("#endTime").val()){
				 $.scojs_message("启始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
		search_khName=$("#khName").val();
		search_startTime=$("#startTime").val();
		search_endTime=$("#endTime").val();
		search_hfName=$("#hfName").val();
		offset=0;
		initdata(false);
		
	});
	//滚动
//	$(window).scroll(function () {
//        //$(window).scrollTop()这个方法是当前滚动条滚动的距离
//        //$(window).height()获取当前窗体的高度
//        //$(document).height()获取当前文档的高度
//        var bot = 0; //bot是底部距离的高度
//        if ((bot + $(window).scrollTop()) >= ($(document).height() - $(window).height())) {
//           //当底部基本距离+滚动的高度〉=文档的高度-窗体的高度时；
//        	if(pageLoading){
//        		pageLoading=false;
//        		offset+=limit;
//        		initdata(true);
//        	}
//        }
//    });
	$(document).on("keydown","body",function(){
		if(window.event.keyCode == 13){
			$("#searchButton").click();
		}
	});
	$(document).on("click",".orderNumber",function(){
		window.location=ctx+"/order/detail/"+$(this).attr("data-val");
	});

});



function initdata(isappend){
//	showLoading();
	/*	var search_param=null;
	var search_gradeId=[];
	var search_startTime=null;
	var search_endTime=null;
	var search_type=[];*/
	$.ajax({
        url : ctx+'/phonevisit/listData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"khName":search_khName,
        	"hfName":search_hfName,
        	"startTime":search_startTime,
        	"endTime":search_endTime,
        	"pager.offset":offset,
        	"limit":limit
        },
        success : function(result){
        	var newcontent = '';
        	var totalElements=result.totalElements;
        	$("#NoData").text("");
        	
        	if(totalElements>0){
        		$(result.content).each(function(index,data){
        			
        			var driverSatisfy="";
        			if(data.driverSatisfy==0){
        				driverSatisfy="<font style='color:red;font-weight:700;'>不满意</font>";
        			}else if(data.driverSatisfy==1){
        				driverSatisfy="满意";
        			}else{
        				driverSatisfy="<font style='color:green;font-weight:700;'>非常满意</font>";
        			}
        			var serviceSatisfy="";
        			if(data.serviceSatisfy==0){
        				serviceSatisfy="<font style='color:red;font-weight:700;'>不满意</font>";
        			}else if(data.serviceSatisfy==1){
        				serviceSatisfy="满意";
        			}else if(data.serviceSatisfy==1){
        			    serviceSatisfy="<font style='color:green;font-weight:700;'>非常满意</font>";
        		    }
        			var chargeSatisfy="";
        			if(data.chargeSatisfy==0){
        				chargeSatisfy="<font style='color:red;font-weight:700;'>不满意</font>";
        			}else if(data.chargeSatisfy==1){
        				chargeSatisfy="满意";
        			}else if(data.chargeSatisfy==1){
        				chargeSatisfy="<font style='color:green;font-weight:700;'>非常满意</font>";
        		    }else if(data.chargeSatisfy==null){
        		    	chargeSatisfy="-";
        		    }
        			var otherSatisfy="";
        			if(data.otherSatisfy==0){
        				otherSatisfy="<font style='color:red;font-weight:700;'>不满意</font>";
        			}else if(data.otherSatisfy==1){
        				otherSatisfy="满意";
        			}else if(data.otherSatisfy==2){
        				otherSatisfy="<font style='color:green;font-weight:700;'>非常满意</font>";
        		    }else if(data.otherSatisfy==null){
        		    	otherSatisfy="-";
        		    }
        			if(data.otherSatisfyInput!=null){
        				otherSatisfy+="("+data.otherSatisfyInput+")";
        			}
        			
	    newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
						"<td>"+(data.created!=null?new Date(data.created).Format("yyyy-MM-dd hh:mm"):"-")+"</td>"+
						"<td><a href='javascript:;' data-val='"+data.orderId+"'  class='btn-link orderNumber' >"+(data.orderNumber==null?"-":data.orderNumber)+"</a></td>"+
						"<td>"+driverSatisfy+"</td>"+
						"<td>"+serviceSatisfy+"</td>"+
						"<td>"+chargeSatisfy+"</td>"+
						"<td>"+otherSatisfy+"</td>"+
						"<td>"+(data.memberName!=null&&data.memberName.length>0?data.memberName:"-")+"</td>"+
						"<td>"+(data.accountName!=null&&data.accountName.length>0?data.accountName:"-")+"</td>"+
						"<td>"+(data.memo==null?"-":data.memo)+"</td></tr>";
					
        		});
        	}
        	else{
              	 $("#NoData").text("当前页面暂无记录 ");
            }

//        	hideLoading();
        	
        	if(!isappend){
        		$(".table tbody .grid-data").remove();
        	}
        	$(".table tbody").append(newcontent);
        	
        	$(".table tbody .grid-data").each(function(index,element){
        		$(element).fadeIn(200+(index*35));
        	});
        	
        	if(!hasShowPages){
        		if(!isappend){
            		currentTotalElements=result.numberOfElements;
            		pageLoading=true;
            	}
        		else{
            		if(currentTotalElements<totalElements){
            			currentTotalElements+=result.numberOfElements;
            			pageLoading=true;
            		}
            		else{
            			pageLoading=false;
            		}
            	}
        	}

        	if(hasShowPages){
    			firstLoadPages=true;
        		$(".pageshow li a").html("共"+totalElements+" 条数据");
        		$(".pagedata").paging(totalElements, {
        			format: '[< ncnnn >]',
        			perpage: pagingLimit,
        			page:(offset/limit)+1,
        			onSelect: function (page) {
        				limit=pagingLimit;
        				offset=(page-1)*limit;
        				if(!firstLoadPages){
        					initdata(false);
        				}
        				firstLoadPages=false;
        		    	return false;
        			},
        			onFormat: function (type) {
        				switch (type) {
        				case 'block': // n and c
        					if (!this.active)
        						return '';
        					else if (this.value != this.page)
        						return '<li><a href="#">' + this.value + '</a></li>';
        					return '<li class="active"><a href="#">' + this.value + '</a></li>';
        				case 'next': // >
        					return '<li><a href="#">&rsaquo;</a><li>';
        				case 'prev': // <
        						return '<li><a href="#">&lsaquo;</a><li>';
        				case 'first': // [
        						return '<li><a href="#">&laquo;</a><li>';
        				case 'last': // ]
        						return '<li><a href="#">&raquo;</a></li>';
        				}
        			}
        		});
        		
        	}
        	else{
        		//滚动达到pagingLimit条开始显示分页
//        		if(currentTotalElements==pagingLimit){
            		
            		pageLoading=false;
            		hasShowPages=true;
            		
            		$(".pageshow").show();
            		$(".pagedata").show();
            		$(".pageshow li a").html("共"+totalElements+" 条数据");
            		$(".pagedata").paging(totalElements, {
            			format: '[< ncnnn >]',
            			perpage: pagingLimit,
            			onSelect: function (page) {
            				limit=pagingLimit;
            				offset=(page-1)*limit;
            				if(!firstLoadPages){
            					initdata(false);
            				}
            				firstLoadPages=false;
            		    	return false;
            			},
            			onFormat: function (type) {
            				switch (type) {
            				case 'block': // n and c
            					if (!this.active)
            						return '';
            					else if (this.value != this.page)
            						return '<li><a href="#">' + this.value + '</a></li>';
            					return '<li class="active"><a href="#">' + this.value + '</a></li>';
            				case 'next': // >
            					return '<li><a href="#">&rsaquo;</a><li>';
            				case 'prev': // <
            						return '<li><a href="#">&lsaquo;</a><li>';
            				case 'first': // [
            						return '<li><a href="#">&laquo;</a><li>';
            				case 'last': // ]
            						return '<li><a href="#">&raquo;</a></li>';
            				}
            			}
            		});
            		
//            	}
        	}
        	
        }
	});
	
}

Date.prototype.Format = function(fmt) 
{ 
  var o = { 
    "M+" : this.getMonth()+1,                 //月份 
    "d+" : this.getDate(),                    //日 
    "h+" : this.getHours(),                   //小时 
    "m+" : this.getMinutes(),                 //分 
    "s+" : this.getSeconds(),                 //秒 
    "q+" : Math.floor((this.getMonth()+3)/3), //季度 
    "S"  : this.getMilliseconds()             //毫秒 
  }; 
  if(/(y+)/.test(fmt)) 
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o) 
    if(new RegExp("("+ k +")").test(fmt)) 
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  return fmt; 
}
